$(function(){
    let nn = document.querySelector("nav>div>.n1");
    let snav = "";

    fetch("http://chst.vip:1234/api/getbaicaijiatitle")
    .then(body=>body.json())
    .then(res=>{
        console.log(res);
        res.result.forEach(item => {
            snav += `<li titleid=${item.titleId}>${item.title}</li>`
        });
        nn.innerHTML = snav;

        var oli = document.querySelectorAll("nav>div>.n1>li");
        console.log(oli)
        for(var i=0; i<oli.length; i++){
            oli[i].onclick = function(){
                var titleid = this.getAttribute("titleid")
                Info(titleid)
            }
        }
    })

})


//获取商品列表
function Info(n){
    let main = document.querySelector("#main>.ulbox");
    console.log(main)
    var str = "";
    fetch("http://chst.vip:1234/api/getbaicaijiaproduct?titleid="+n)
    .then(body=>body.json())
    .then(res=>{
        console.log(res);
        res.result.forEach(item=>{
            let regExp = /(\/|http)[^>]+\.(jpg|jpeg|png|gif)/g
            let r = item.productImg.match(regExp)
            str +=`<li>
                        <a class="im">
                            <div class="img">
                                <img src="${r[0]}">
                            </div>
                            <div class="info">
                                <div class="title">
                                    <h4>${item.productName}</h4>
                                    <div class="price">${item.productPrice}</div>
                                </div>
                                <div class="down">
                                    <div class="bar">
                                        <div class="bar_in" style="width: 0px;">0%</div>
                                    </div>
                                    <span class="yiling">已领126张/剩余0张</span>
                                    <div class="click">
                                        <div class="yhq">点此领取优惠券</div>
                                        <div class="xdlj">下单链接</div>
                                    </div>
                                </div>
                            </div>
                        </a>
                    </li>
                    `
        })
        main.innerHTML=str;
        
        $('.bar div').animate({ width: '100%' }, 3000, 'linear')
            let p = 0;
            let time = setInterval(function () {
                p++;
                $("#main .ulbox li .im .info .down .bar_in").html(p + "%")
                if (p === 100) {
                    clearInterval(time)
                }
            }, 25)
            let p1 = 0;
            let p2 = 126;
            let time1 = setInterval(function () {
                p1++;
                p2--;
                $("#main .ulbox li .im .info .down .yiling").html("已领" + p1 + "张/剩余" + p2 + "张")
                if (p1 === 126) {
                    clearInterval(time1)
                }
            }, 25)
    })
}
Info(0)